##    This file is part of The Most Awesome Story Ever
##
##    Foobar is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    Foobar is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
##    
##    Copyright 2008, Ryan Michela

from google.appengine.ext import db
import cgi

class Page(db.Model):
    """This class is the model for all Page database records."""
    
    pageNum = db.IntegerProperty(required=True)
    title = db.StringProperty(multiline=False)
    body = db.TextProperty()
    image = db.BlobProperty()
    imageExtension = db.StringProperty(multiline=False)
    optionTitles = db.StringListProperty()
    optionPageNums = db.ListProperty(int)
    
    def getOptions(self):
        """Fetch the options from this page; returns a dictionary mapping 
        option names (string) to page numbers (int)."""
        
        options = {}
        for i in range(self.getOptionCount()):
            options[self.optionTitles[i]] = self.optionPageNums[i]
        return options
    
        
    def getOptionCount(self):
        """Return the number of options for this page as an int."""
        return len(self.optionTitles)
        
                
    def addOption(self, optionTitle, optionPageNum):
        """Add an option with associated page number to this page."""
        self.optionTitles.append(cgi.escape(optionTitle))
        self.optionPageNums.append(optionPageNum)
        
    def isEmpty(self):
        """Determine if this page has been populated or not; return bool."""
        if(self.title == None):
            return True
        else:
            return False
    
